﻿@model IEnumerable<wdsDeApp03.Models.ProInfo>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("添加", "Create")
</p>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
    <span>物资类型</span>
    @Html.DropDownList("type", ViewBag.dropDownList as SelectList)
    <span>物资名称</span>
    @Html.TextBox("text")
    <input type="submit" value="查询" />
}
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AddTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.IsDestroy)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Unit)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Type)
        </th>
        <th>
            操作
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AddTime)
            </td>
            <td>
                @if (!Convert.ToBoolean(item.IsDestroy))
                {
                    @:未报废
                }
                else
                {
                    @:已报废
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Unit)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @{
                    var itemName = (ViewBag.typeInfo as Dictionary<int, string>).FirstOrDefault(u => u.Key == item.Type).Value;
                    @itemName
                }
            </td>
            <td>
                @if (!Convert.ToBoolean(item.IsDestroy))
                {
                    <button onclick="Update(@item.ID)">报废</button>
                }
            </td>
        </tr>
    }

</table>
@section Scripts {
    <script>
        function Update(id) {
            if (confirm("是否报废编号为" + id + "的物资?")) {
                $.post("/Home/Update", { uid: id }, function (data) {
                    switch (data) {
                        case 0:
                            console.log("报废失败");
                            break;
                        case 1:
                            alert("报废成功");
                            location = "/Home/Index";
                            break;
                        case 2:
                            console.log("错误");
                            break;
                    }
                })
            }
        }
    </script>
}

